/*
 * Copyright (C) 2017-2021
 * All rights reserved, Designed By
 * Copyright authorization contact 18814114118
 */
package com.shop.cereshop.app.service.buyer.impl;

import cn.hutool.core.collection.CollUtil;
import com.shop.cereshop.app.dao.buyer.CereBuyerSearchDAO;
import com.shop.cereshop.app.domain.buyer.BuyerSearchVO;
import com.shop.cereshop.app.service.buyer.CereBuyerSearchService;
import com.shop.cereshop.commons.domain.buyer.CereBuyerSearch;
import com.shop.cereshop.commons.exception.CoBusinessException;
import com.shop.cereshop.commons.result.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Slf4j(topic = "CereBuyerSearchServiceImpl")
@Service
public class CereBuyerSearchServiceImpl implements CereBuyerSearchService {

    @Autowired
    private CereBuyerSearchDAO cereBuyerSearchDAO;

    @Override
    public List<CereBuyerSearch> getHistory(Long buyerUserId) {
        return cereBuyerSearchDAO.getHistory(buyerUserId);
    }

    @Override
    public void insert(CereBuyerSearch cereBuyerSearch) throws CoBusinessException {
        cereBuyerSearchDAO.insert(cereBuyerSearch);
    }

    @Async
    @Override
    public void save(Long buyerUserId, String keyword, Integer searchType) {
        CereBuyerSearch searchRecord = new CereBuyerSearch();
        searchRecord.setBuyerUserId(buyerUserId);
        searchRecord.setSearch(keyword);
        searchRecord.setSearchType(searchType);
        cereBuyerSearchDAO.insert(searchRecord);
    }

    @Override
    public void delete(Long searchId) {
        cereBuyerSearchDAO.deleteByPrimaryKey(searchId);
    }

    @Override
    public CereBuyerSearch findBySearch(String search, Long buyerUserId,Integer searchType) {
        return cereBuyerSearchDAO.findBySearch(search,buyerUserId,searchType);
    }

    @Override
    public void updateBuyerData(Long buyerUserId, Long id) throws CoBusinessException {
        cereBuyerSearchDAO.updateBuyerData(buyerUserId,id);
    }

    @Override
    public List<String> selectHotSearch() {
        return cereBuyerSearchDAO.selectHotSearch();
    }

    @Override
    public Result<List<BuyerSearchVO>> getBuyerSearchList(Long buyerUserId, Integer searchType) {
        List<BuyerSearchVO> searchList = cereBuyerSearchDAO.getUserSearchList(buyerUserId, searchType);
        if (CollUtil.isEmpty(searchList)) {
            log.info("用户: {}没有搜索记录, searchType: {}", buyerUserId, searchType);
            return Result.success("没有searchType="+ searchType +"的搜索记录", Collections.emptyList());
        }
        return Result.success(searchList);
    }

    @Override
    public Result<List<BuyerSearchVO>> getUserHotSearch(Long buyerUserId, Integer searchType) {
        List<BuyerSearchVO> searchList = cereBuyerSearchDAO.getUserHotSearch(buyerUserId, searchType);
        if (CollUtil.isEmpty(searchList)) {
            log.info("用户: {}没有搜索记录, searchType: {}", buyerUserId, searchType);
            return Result.success("没有searchType="+ searchType +"的搜索记录", Collections.emptyList());
        }
        if (searchList.size() > 10) {
            searchList = getRandomSearches(searchList, 10);
        }
        return Result.success(searchList);
    }

    /**
     * 获取随机样本
     *
     * @param list 列表
     * @param n    数量
     * @return 随机样本
     */
    public List<BuyerSearchVO> getRandomSearches(List<BuyerSearchVO> list, int n) {
        if (list == null || list.isEmpty() || n <= 0) {
            return Collections.emptyList();
        }
        List<BuyerSearchVO> copy = new ArrayList<>(list);
        Collections.shuffle(copy);
        return new ArrayList<>(copy.subList(0, Math.min(n, copy.size())));
    }

    @Override
    public Result<Void> deleteBuyerSearch(Long buyerUserId, Long searchId) {
        cereBuyerSearchDAO.deleteBuyerSearch(buyerUserId, searchId);
        return Result.success();
    }

    @Override
    public Result<Void> deleteBuyerAllSearch(Long buyerUserId, Integer searchType) {
        cereBuyerSearchDAO.deleteBuyerSearchBySearchType(buyerUserId, searchType);
        return Result.success();
    }


}

